﻿using System;
using System.Data.SqlClient;
using System.Data;
using Grishko.DataTier.SqlLibrary;
using Grishko.DataTier;
using OutdoorLib.BaseEntity.Constraction;

namespace OutdoorLib.BaseEntity.Constraction
{

    /// <summary>
    /// Список возвращает xml соответсвующий SideItemBox
    /// </summary>
    public class SideLst : SqlStatmentPropertyOf_PagingList
    {
        public class SideItemBox : EntitySingle
        {
            public SideItemBox() { }


            private Side _Side = null;
            public Side Side { get { return _Side; } set { _Side = value; } }

            private ConstractionEntity _Constraction = null;
            public ConstractionEntity Constraction { get { return _Constraction; } set { _Constraction = value; } }
        }
        public class sqlLoadStatment : ListLoadStatment
        {
            public override string vPropertyOf_ViewName
            {
                get
                {
                    return "v_srch_PropertyOfSide";
                }
            }

            public override string Source_ViewName
            {
                get
                {
                    return "v_Sides";
                }
            }


            protected override string Do_PageSet_Sql_5()
            {
                return "";
            }
            protected override string Do_PageSetToXml_Sql_6()
            {
                return "select v_sides.id as SideID,Constraction.id as ConstractionID,	v_sides.Xml.value('(/Side/@Litera)[1]', 'nvarchar(50)') Litera,	Constraction.Xml.value('(/Constraction/@GID)[1]', 'nvarchar(50)') Number into #PAGE from v_sides left join 	v_Link_ConstractionSide link on link.SideId=v_sides.Id	join v_Constraction  Constraction on Constraction.ID=link.ConstractionID join #SelectList  on #SelectList.ID=v_sides.ID select count(*) from #Page select 	v_sides.xml as Side,	Constraction.xml as Constraction,(select [xml].query('.') from v_ContractSurface  where SideID=v_sides.ID FOR XML PATH(''), TYPE, ELEMENTS , ROOT('EntityList') ) as SheduleList	 from 	(select #page.*,  ROW_NUMBER() OVER (ORDER BY #page.Number,#page.Litera) AS row_num  from #page) as Page join v_Constraction Constraction on Constraction.id=Page.ConstractionID 	join v_sides on v_sides.id=Page.SideID where row_num between " + _StartRowIndex.ToString() + " and " + _EndRowIndex.ToString() + " ";
            }

            

        }
        public override void Load()
        {
            SecuretyContext SC = AppContext.GetSC();
            try
            {
                if (null == _SqlStatment) return;

                //_SqlStatment.PrepareStatment();
                string sql = _SqlStatment.GetStatment();

                System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
                cmd.CommandText = sql;
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Connection = SC.Connection;
                SC.Connection.Open();
                                
                SqlDataAdapter adp = new SqlDataAdapter (cmd);
                DataSet ds= new DataSet ();
                adp.Fill (ds);
                DataTable tbl = ds.Tables[1];               
 
                foreach (DataRow rw in tbl.Rows )
                {
                    Side side = (Side)EntitySingle.GetEntity(rw[0].ToString());
                    ConstractionEntity constr = (ConstractionEntity)EntitySingle.GetEntity(rw[1].ToString());
                    SideItemBox i = new SideItemBox() { Constraction = constr, Side = side };
                    this.Add(i);
                }

                tbl = ds.Tables[0];               
                this.OutputRwCnt = Convert.ToInt16(tbl.Rows[0][0].ToString()) ;
            }
            catch
            {
                return;
            }
            finally
            {
                SC.Connection.Close();
            }
        }
    }
}